//
//  HomePickerView.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/30.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "HomePickerView.h"

@interface HomePickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>{
    NSArray * listArr;
    //时间选择器
    UIDatePickerMode _datePickerMode;
    NSString *_selectValue;//选择的时间

}
@property (nonatomic,strong) GLGWView * pickViewBankgroundView;
@property (nonatomic,strong) UIPickerView * pickerView;
@property (nonatomic,strong) UIDatePicker * datePickerView;


@end

@implementation HomePickerView
-(id)initWithPickerView:(PickerViewType)p_Type{
    if (self = [super initWithFrame:kAppWindow.bounds inSuperView:kAppWindow]) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];//145
        CGFloat pickViewHeight;
        if (iPhoneX) {
            pickViewHeight = kHeightProportion_In_iPhoneTriploid(305);
        }else{
            pickViewHeight = kHeightProportion_In_iPhoneTriploid(271);
        }
        //
        self.pickViewBankgroundView = [[GLGWView alloc] initWithFrame:CGRectMake(0, KScreenHeight, KScreenWidth, pickViewHeight)  inSuperView:self];
        self.pickViewBankgroundView.backgroundColor  = [UIColor whiteColor];
        [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:3 options:UIViewAnimationOptionCurveEaseIn animations:^{
            self.pickViewBankgroundView.frame = CGRectMake(0, KScreenHeight - pickViewHeight, KScreenWidth, pickViewHeight);
        } completion:^(BOOL finished) {
            
        }];
        //
        GLGWButton * quxiaoBut = [[GLGWButton alloc] initWithFrame:CGRectMake(0, 0, kWidthProportion_In_iPhoneTriploid(60), kHeightProportion_In_iPhoneTriploid(60)) hTitle:@"取消" titleLableFont:SYSTEMFONT(14) titleLableColor:kColor_Rule_Yellow theCornerRadius:0 backgroundColr:[UIColor whiteColor] inSuperView:self.pickViewBankgroundView];
        quxiaoBut.tag = 160;
        [quxiaoBut addTarget:self action:@selector(pickViewAction:) forControlEvents:UIControlEventTouchUpInside];
        //
        GLGWButton * comintBut = [[GLGWButton alloc] initWithFrame:CGRectMake(KScreenWidth - kWidthProportion_In_iPhoneTriploid(60), 0, kWidthProportion_In_iPhoneTriploid(60), kHeightProportion_In_iPhoneTriploid(60)) hTitle:@"完成" titleLableFont:SYSTEMFONT(14) titleLableColor:kColor_Rule_Yellow theCornerRadius:0.0 backgroundColr:[UIColor whiteColor] inSuperView:self.pickViewBankgroundView];
        comintBut.tag = 161;
        [comintBut addTarget:self action:@selector(pickViewAction:) forControlEvents:UIControlEventTouchUpInside];
        //
        if (p_Type == PickerViewType_Stell) {
            listArr = @[@"管材",@"优特钢",@"建筑钢材",@"热卷",@"中厚板"];
            self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(40), KScreenWidth, kHeightProportion_In_iPhoneTriploid(231))];
            self.pickerView.delegate=self;
            self.pickerView.dataSource=self;
            self.pickerView.backgroundColor=[UIColor whiteColor];
            [self.pickViewBankgroundView addSubview:self.pickerView];
        }else{
            self.datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, kHeightProportion_In_iPhoneTriploid(40), KScreenWidth, kHeightProportion_In_iPhoneTriploid(231))];
            self.datePickerView.backgroundColor = [UIColor whiteColor];
            self.datePickerView.datePickerMode = UIDatePickerModeDate;
            _datePickerMode = UIDatePickerModeDate;
            // 设置该UIDatePicker的国际化Locale，以简体中文习惯显示日期，UIDatePicker控件默认使用iOS系统的国际化Locale
            //            _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CHS_CN"];
            //            _datePicker.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_GB"];
            [self.datePickerView setLocale:[NSLocale currentLocale]];
//            // 设置时间范围
//            if (_minDateStr) {
//                //                NSDate *minDate = [self toDateWithDateString:_minDateStr];
//
//            }
//            _datePicker.minimumDate = [NSDate date];
//            if (_maxDateStr) {
//                NSDate *maxDate = [self toDateWithDateString:_maxDateStr];
//                _datePicker.maximumDate = maxDate;
//            }
            // 把当前时间赋值给 _datePicker
            [self.datePickerView setDate:[NSDate date] animated:YES];
            
            // 滚动改变值的响应事件
            [self.datePickerView addTarget:self action:@selector(didSelectValueChanged:) forControlEvents:UIControlEventValueChanged];
            // 默认选中今天的日期
            _selectValue = [self toStringWithDate:[NSDate date]];
            [self.pickViewBankgroundView addSubview:self.datePickerView];
        }
        
    }
    return self;
}

#pragma mark =================== 时间选择器 ===================
- (void)didSelectValueChanged:(UIDatePicker *)sender {
    // 读取日期：datePicker.date
    _selectValue = [self toStringWithDate:sender.date];
    NSLog(@"滚动完成后，执行block回调:%@", _selectValue);
    // 设置是否开启自动回调
}

#pragma mark - 格式转换：NSDate --> NSString
- (NSString *)toStringWithDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    switch (_datePickerMode) {
        case UIDatePickerModeTime:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        case UIDatePickerModeDate:
            [dateFormatter setDateFormat:@"yyyy-MM"];
            break;
        case UIDatePickerModeDateAndTime:
            [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
            break;
        case UIDatePickerModeCountDownTimer:
            [dateFormatter setDateFormat:@"HH:mm"];
            break;
        default:
            break;
    }
    NSString *destDateString = [dateFormatter stringFromDate:date];
    
    return destDateString;
}

-(void)pickViewAction:(GLGWButton *)sender{
    if (sender.tag == 161) {
        
    }
    [self remove_self];
}

-(void)remove_self{
    [UIView animateWithDuration:0.25 animations:^{
        self.pickViewBankgroundView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

#pragma mark =================== UIPickerViewDataSource,UIPickerViewDelegate ===================
//  设置对应的字体大小
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    // 设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = [UIColor colorWithHexString:@"#EAEAEA"];
        }
    }
    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, (KScreenWidth-30)/3,40)];
    label.textAlignment=NSTextAlignmentCenter;
    label.font=[UIFont systemFontOfSize:16];
    label.text=[self pickerView:pickerView titleForRow:row forComponent:component]; // 数据源
    return label;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

//多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return listArr.count;
}

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString * title = nil;
    title = listArr[row];
    return title;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    //滚动一区的时候
//    if (component==0) {
//        CityModel_CityList *Prmodel =cityModelArr_province[row];
//        cityModelArr_city=Prmodel.city;
//        [pickerView reloadComponent:1];
//        [pickerView selectRow:0 inComponent:1 animated:YES];
//
//        CityModel_City * ciModel = cityModelArr_city[0];
//        cityModelArr_zone=ciModel.area;
//        [pickerView reloadComponent:2];
//        [pickerView selectRow:0 inComponent:2 animated:YES];
//
//
//        //滚动二区的时候
//    } else if (component==1) {
//        CityModel_City * ciModel = cityModelArr_city[row];
//        cityModelArr_zone=ciModel.area;
//        [pickerView reloadComponent:2];
//        [pickerView selectRow:0 inComponent:2 animated:YES];
//
//    }
    DLog(@"----------------      %@",listArr[row]);
}



@end
